//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "Ligand.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHTEXT 100
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
Ligand::Ligand () {
  
  this->name      = new String ();
  this->number    = 0;
  
  this->records   = new TListE <HetAtmRecord> ();

  this->toString  = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
Ligand::Ligand (const Ligand& ligand) {
  
  this->name      = new String (ligand.name);
  this->number    = ligand.number;
  
  this->records   = new TListE <HetAtmRecord> (ligand.records);

  this->toString  = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
Ligand::Ligand (const Ligand* ligand) {
  
  this->name      = new String (ligand->name);
  this->number    = ligand->number;
  
  this->records   = new TListE <HetAtmRecord> (ligand->records);

  this->toString  = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
Ligand::~Ligand () {
  
  if (this->name)     delete this->name;
  if (this->records)  delete this->records;
  if (this->toString) delete this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void Ligand::Name (String* name) {
  
  if (name) this->name->In(name);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void Ligand::Number (int number) {
  
  this->number = number;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void Ligand::Records (TListE <HetAtmRecord>* records) {
  
  if (records) *(this->records) = *(records);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* Ligand::Name (void) {
  
  return this->name;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int Ligand::Number (void) {
  
  return this->number;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <HetAtmRecord>* Ligand::Records (void) {
  
  return this->records;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool Ligand::ReadData (String* data) {
  
  if (!data || !data->Length()) return false;
  
  this->CleanData();
  
  char   tmpData [data->Length() + 1];
  char*  lines = NULL;
  
  strcpy(tmpData, data->Out());
  tmpData[data->Length()] = '\0';

  lines = strtok(tmpData, "\n");
  
  while (lines) {

    HetAtmRecord* newHetAtmRecord = new HetAtmRecord ();
    
    if (newHetAtmRecord->ReadFromString(lines)) 
      this->records->Add(newHetAtmRecord);
    else
      delete newHetAtmRecord;
    
    lines = strtok(NULL, "\n");

  }
  
  if (!this->records->Length()) return false;
  
  this->name->In(this->records->GetFirst()->Name());
  this->number = this->records->GetFirst()->Number();
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool Ligand::ReadData (char* data) {
  
  if (!data || !strlen(data)) return false;
  
  String dataChar;
  dataChar.In(data);
  
  return this->ReadData(&dataChar);    

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* Ligand::ToString (void) {
  
  this->toString->In("");
  
  this->records->SetInitial();
  while (this->records->SetNext()) {
    this->toString->Add(this->records->GetCurrent()->ToString());
    this->toString->Add("\n");
  }
  
  return this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void Ligand::CleanData (void) {
  
  this->name->In("");
  this->number = 0;
  
  this->records->Clear();
  this->toString->In("");
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
Ligand Ligand::operator= (const Ligand& ligand) {
  
  this->name->In(ligand.name);
  
  this->number     = ligand.number;
  *(this->records) = *(ligand.records);

  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
